package com.duowei.diandan.db.dao;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;

import com.duowei.diandan.db.entry.Wmlsb;

import java.util.List;

@Dao
public interface WmlsbDao {

    @Query("SELECT * FROM wmlsb WHERE isMain = '1'")
    List<Wmlsb> findMainList();

    @Query("SELECT * FROM wmlsb WHERE isMain = '0' AND mainId = :mainId")
    List<Wmlsb> findChildList(long mainId);

    // ----------

    @Query("SELECT * FROM wmlsb WHERE wmdbh = :wmdbh")
    List<Wmlsb> findByWmdbh(String wmdbh);

    @Query("SELECT * FROM wmlsb WHERE by6 = '1'")
    List<Wmlsb> findByBy6EqualOne();

    @Query("SELECT pz FROM wmlsb WHERE xmbh = :xmbh LIMIT 1")
    String findPzByXmbh(String xmbh);

    /**
     * 查询套餐子项列表
     *
     * @param tcbh 套餐编号
     */
    @Query("SELECT * FROM wmlsb WHERE tcbh = :tcbh and (by15 != 'A' or by15 IS NULL)")
    List<Wmlsb> findMealChildByTcbh(String tcbh);

    /**
     * 查询套餐子项列表
     *
     * @param tcbh
     */
    @Query("SELECT * FROM wmlsb WHERE (by6 != '1' OR by6 IS NULL) AND tcbh = :tcbh")
    List<Wmlsb> findChildByTcbh(String tcbh);

    @Query("SELECT * FROM wmlsb WHERE sfxs = '1'")
    List<Wmlsb> findAllSfxs1();

    @Query("SELECT * FROM wmlsb WHERE by1 = :by1 LIMIT 1")
    Wmlsb findOneByBy1(String by1);

    @Query("DELETE FROM wmlsb WHERE by1 = :by1")
    void deleteByBy1(String by1);

    @Query("SELECT * FROM wmlsb")
    List<Wmlsb> findAll();

    @Query("SELECT * FROM wmlsb WHERE xh IS NOT NULL")
    List<Wmlsb> findAllRemote();

    @Query("SELECT * FROM wmlsb WHERE xh IS NULL")
    List<Wmlsb> findAllLocal();

    @Query("SELECT * FROM wmlsb WHERE id = :id LIMIT 1")
    Wmlsb findOneById(long id);

    @Query("SELECT * FROM wmlsb WHERE xmbh = :xmbh LIMIT 1")
    Wmlsb findOneByXmbh(String xmbh);

    @Insert
    long insert(Wmlsb item);

    @Insert
    void insert(List<Wmlsb> list);

    @Update
    void update(Wmlsb item);

    @Update
    void update(List<Wmlsb> list);

    @Delete
    void delete(Wmlsb item);

    @Delete
    void delete(List<Wmlsb> list);

    @Query("DELETE FROM wmlsb")
    void deleteAll();

    @Query("DELETE FROM wmlsb WHERE xh IS NULL")
    void deleteAllLocal();
}
